﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;


namespace FSC.HaNamAPApplication
{
    public partial class HNAP_AdminAPEdit : System.Web.UI.UserControl
    {
        private string modeform
        {
            get{
                string modeform = "a";
                if (Request.QueryString["m"] != null && Request.QueryString["m"].ToString() != "")
                {
                    modeform = Request.QueryString["m"].ToString();
                }
                return modeform;
            }
        }

        private int apid
        {
            get
            {
                int apid = -1;
                if (Request.QueryString["aid"] != null && int.TryParse(Request.QueryString["aid"].ToString(),out apid))
                {
                    apid = int.Parse(Request.QueryString["aid"].ToString());
                }
                return apid;
            }
        }
        
        public string CurrentUrlWithoutParam
        {
            get
            {
                return Controler.UrlWithoutParam(Request.RawUrl);
            }
        }

        protected int number = 1;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitForm();
                BindDrop();

                
                BindDropDocsDialog();

                if (this.modeform.Equals("u"))
                    LoadData();
            }
        }

        private void BindDrop()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            
            // Bind Fields
            var dataField = from f in db.Fields
                            orderby f.Name
                            select f;
            ddlField.DataValueField = "Field_ID";
            ddlField.DataTextField = "Name";
            ddlField.DataSource = dataField;
            ddlField.DataBind();

            ddlField.Items.Insert(0, new ListItem("--Chọn lĩnh vực--", "-1"));

            // Bind Offices
            var dataOffice = from o in db.Offices
                             orderby o.Name
                             select new
                             {
                                 o.Office_ID,
                                 o.Name
                             };
            ddlOffice.DataValueField = "Office_ID";
            ddlOffice.DataTextField = "Name";
            ddlOffice.DataSource = dataOffice;
            ddlOffice.DataBind();

            ddlOffice.Items.Insert(0, new ListItem("--Chọn cơ quan--", "-1"));
        }

        private void LoadData()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            AdminProcedure objAP = db.AdminProcedures.SingleOrDefault(a => a.AP_ID == this.apid);
            if (objAP != null)
            {
                txtName.Text = objAP.Name;
                ddlField.SelectedValue = objAP.Field_ID.ToString();
                ddlOffice.SelectedValue = objAP.ExecuteOffice_ID.ToString();

                txtSteps.Text = Server.HtmlDecode(objAP.Steps);
                txtCompositionOfRecord.Text = Server.HtmlDecode(objAP.CompositionOfRecord);
                txtNumberOfRecord.Text = objAP.NumberOfRecords;
                txtTimeLimitPerform.Text = objAP.TimeLimitPerform;
                txtSubjectPerform.Text = objAP.SubjectsPerform;
                txtResultPerformAP.Text = objAP.ResultPerformAP;
                txtFee.Text = objAP.Fee;
                txtRequestAP.Text = Server.HtmlDecode(objAP.RequestAP);
                //txtLegalGrounds.Text = Server.HtmlDecode(objAP.LegalGrounds);
                txtHowToPerform.Text = objAP.HowToPerform;

                cbEffectStatus.Checked = objAP.EffectStatus;
                cbCommentStatus.Checked = objAP.CommentStatus;
            }
        }

        private void UpdateData()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);
            AdminProcedure objAP;

            if (this.modeform.Equals("u"))
                objAP = db.AdminProcedures.SingleOrDefault(a => a.AP_ID == this.apid);
            else
                objAP = new AdminProcedure();

            if (objAP == null) return;

            objAP.Name = txtName.Text;
            objAP.Field_ID = int.Parse(ddlField.SelectedValue);
            objAP.ExecuteOffice_ID = int.Parse(ddlOffice.SelectedValue);
            objAP.EffectStatus = cbEffectStatus.Checked;
            objAP.CommentStatus = cbCommentStatus.Checked;
            objAP.Steps = txtSteps.Text;
            objAP.CompositionOfRecord = txtCompositionOfRecord.Text;
            objAP.NumberOfRecords = txtNumberOfRecord.Text;
            objAP.TimeLimitPerform = txtTimeLimitPerform.Text;
            objAP.SubjectsPerform = txtSubjectPerform.Text;
            objAP.ResultPerformAP = txtResultPerformAP.Text;
            objAP.Fee = txtFee.Text;
            objAP.RequestAP = txtRequestAP.Text;
            //objAP.LegalGrounds = txtLegalGrounds.Text;
            objAP.HowToPerform = txtHowToPerform.Text;

            if (this.modeform.Equals("a"))
            {
                db.AdminProcedures.InsertOnSubmit(objAP);
            }

            db.SubmitChanges();
            Response.Redirect(CurrentUrlWithoutParam + "?page=AdminAP");
        }

        private void InitForm()
        {
            //CheckBox cbAttachFile = new CheckBox();
            //cbAttachFile.Checked = true;
            //cbAttachFile.ID = "cbAttachFile1";
            //plhAttachFiles.Controls.Add(cbAttachFile);

            //FileUpload fupAttachFile = new FileUpload();
            //fupAttachFile.ID = "fupAttachFile1";
            //plhAttachFiles.Controls.Add(fupAttachFile);
        }

        protected void lbtUpdate_Click(object sender, EventArgs e)
        {
            //UpdateData();

            //System.IO.FileInfo f = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
            //byte[] byteArr = System.IO.File.ReadAllBytes(FileUpload1.PostedFile.FileName);
            //Response.Write(FileUpload1.PostedFile.FileName);

            //Fix for case: use Request.Files
            // add <form enctype="multipart/form-data" ... >
            

            Response.Write(Request.Files.Count);

            if (Request.Files != null)
            {
                if (Request.Files.Count > 0)
                {
                    HttpPostedFile fUpload = Request.Files["fUpload0"];
                    if (fUpload != null)
                        Response.Write(fUpload.FileName);
                    else
                        Response.Write("khong ton tai");
                }
            }
        }

        protected void rptAttachFile_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        
        

        private void BindDropDocsDialog()
        {
            HaNamAPDataDataContext db = new HaNamAPDataDataContext(Controler.HaNamAPConnectionString);

            // Bind Fields
            var dataField = from f in db.Fields
                            orderby f.Name
                            select f;
            ddlDocFields.DataValueField = "Field_ID";
            ddlDocFields.DataTextField = "Name";
            ddlDocFields.DataSource = dataField;
            ddlDocFields.DataBind();

            ddlDocFields.Items.Insert(0, new ListItem("--Không lĩnh vực--", "-1"));

            // Bind Offices
            var dataOffice = from o in db.Offices
                             orderby o.Name
                             select new
                             {
                                 o.Office_ID,
                                 o.Name
                             };
            ddlDocOffices.DataValueField = "Office_ID";
            ddlDocOffices.DataTextField = "Name";
            ddlDocOffices.DataSource = dataOffice;
            ddlDocOffices.DataBind();

            ddlDocOffices.Items.Insert(0, new ListItem("--Không cơ quan--", "-1"));
        }

        
    }
}